package com.api.demo.test.pay_centre;

import cn.hutool.http.ContentType;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.api.demo.util.RsaUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: ym
 * @Description: 通用
 * @Date: 2024/2/22 08:58
 * @Version: 1.0
 */
public class Common {

    //请求地址
    public static String requestUrl = "http://127.0.0.1:10009/api/pay/common/";

    //公钥
    public static String requestPuk = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwvR7PfDtioGM76+m+iMJmDlzUO5uiODSslBKu" +
            "AWN1Oq8vmcW7iVbSAOPQ3GSpAdvXe9+BEyDA6yJ5HABBDsyRfWquLi2ViZ2CV2cFWNGklJQIAs/V" +
            "i6HbE5QMlR+mB08/7sD1gazt+yriV1YSVzMILy9Yl0uOi8bnCZz/DF6wQQIDAQAB";

    //私钥
    public static String respPik = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKJAbgdVOIrPAidjCr9ycbOKeASx" +
            "VtHEclXhTNtoBdzgVii5WHe6EOosE/xEwTbx351SH52lRKEjDEeh+HQ2X9WbtJ46FmJg9NJ+28dg" +
            "jZljSpT1IKbR64tUxLtvCpnQYmruue3SJh5PgdTnhzzmN6v5qAcYKgh4BpRVegt4y6UnAgMBAAEC" +
            "gYBH5rp7uGnapZrkPnFMXtiBQmsmVTpOsjoi0blf/6ydcbLs0Boi0ItfYtImeIeRJEW+r16AW3Va" +
            "3S+7bM0wHsp/p6Jr9mCN/Dhs1n5F+ulefH8xU+hJsPxc0DP7tsyvm0r2T63fjRxO5t4Nra0bF6op" +
            "SDFbogkRm2zT8BO7dcjPSQJBAMzTkxlPbZoAOdKJymf671Za6Cupx1yYCrEZ9ZBJaXAffh6T7Ybh" +
            "wmZUt5BB6goR3SlCb73q1MP6CZgBNgpNXFkCQQDKydRFj4Wc60melycXGpU7QXfBzapnodKVlUHO" +
            "aKIbgsTOdFOFskBayfQfGenZMmrvs25BTLIq26pzpKxTAt1/AkEAnjcD1jYrZByujXmQFneX7m10" +
            "D14qt9Rd1eVGcPiz8D7zTa1YearjXeYskMrJfJPF8Pv3kozVYrg+NuMgmh6dqQJBAL0+rQJddeuD" +
            "CCkgFjLRdALgjconDKe5tLQznj3qF55k4xVoKanTN4uCfHepzzoKexFG+dWmpX6pkOBrBQZS3jEC" +
            "QEW5SNImc73200qoaD3+bc0KuFwEeBYccYz3bJw4Wad2OGHCxmPw8tB0O9WimqUfONFX/GXNcGxc" +
            "ZJtm4flZ894=";

    public static void main(String[] args) {
        System.out.println(respPik);
        //交易详情
//        post(requestUrl + "transactionInfo", transactionInfo());

        //查询回单
//        post(requestUrl + "receiptInfo", receiptInfo());

        //查询退票
//        post(requestUrl + "refundListByDate", refundListByDate());


    }


    /**
     * 交易详情
     *
     * @return
     */
    public static String transactionInfo() {
        Map<String, Object> data = new HashMap<>();
        data.put("businessOrderId", "1708581735787");
        data.put("full", false);
        return encrypt(data);
    }


    /**
     * 查询回单
     *
     * @return
     */
    private static String receiptInfo() {
        Map<String, Object> data = new HashMap<>();
        data.put("businessOrderId", "1671525998207");
        return encrypt(data);
    }

    /**
     * 查询退票
     *
     * @return
     */
    private static String refundListByDate() {
        Map<String, Object> data = new HashMap<>();
        data.put("refundDateStart", "2024-02-01");
        data.put("refundDateEnd", "2024-03-01");
        return encrypt(data);
    }


    public static void post(String url, String body) {
        HttpRequest post = HttpUtil.createPost(url);
        post.header("app-id", "2");
        String result = post.body(body, ContentType.JSON.toString()).execute().body();

        System.out.println(url + " --》请求结果:" + result);

        //判断是否加密
        JSONObject re = JSON.parseObject(result);
        Boolean encrypt = re.getBoolean("encrypt");
        if (encrypt) {
            String decrypt = RsaUtils.decrypt(re.getString("data"), respPik);
            System.out.println("解密结果：" + decrypt);
            System.out.println(decrypt);
        } else {
            System.out.println("无需解密---------");
        }

    }


    public static String encrypt(Map<String, Object> data) {
        System.out.println("JSON.toJSONString(data) = " + JSON.toJSONString(data));
        System.out.println(JSON.toJSONString(data));

        String encrypt = RsaUtils.encrypt(JSON.toJSONString(data), requestPuk);
        Map<String, Object> post = new HashMap<>();
        post.put("data", encrypt);
        post.put("signType", "RSA");
        String jsonString = JSON.toJSONString(post);
        System.out.println("参数-加密数据：" + jsonString);
        return jsonString;
    }


}
